//
//  LocationTool.m
//  JiuyaoDriver
//
//  Created by 大橙子 on 2018/11/24.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "LocationTool.h"
#define DefaultLocationTimeout 10
#define DefaultReGeocodeTimeout 5
@interface LocationTool ()<AMapLocationManagerDelegate>

@property (nonatomic, copy) AMapLocatingCompletionBlock completionBlock;


@property (nonatomic, assign) NSInteger locateCount;

@end
@implementation LocationTool
static LocationTool *_instance;

+ (instancetype)sharedLocationTool
{
    //    if (_instance == nil) {
    _instance = [[LocationTool alloc] init];
    //}
    
    return _instance;
}

- (instancetype)init
{
    if (self = [super init]) {
        
        self.locateCount = 0;
        
        [self initCompleteBlock];
        
        [self configLocationManager];
        
        //停止连续定位
        [self.locationManager stopUpdatingLocation];
        
        self.locateCount = 0;
        
        //进行单次定位
        [self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
    }
    return self;
}

- (void)initCompleteBlock
{
    __weak LocationTool *weakSelf = self;
    self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
    {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            //            [weakSelf.singleLocInfoLabel setText:[NSString stringWithFormat:@"locError:{%ld - %@};", (long)error.code, error.localizedDescription]];
            
            return;
        }
        
        //得到定位信息
        if (location)
        {
            [weakSelf updateLabelTextWithLocation:location regeocode:regeocode serial:NO];
        }
    };
}
- (void)configLocationManager
{
    self.locationManager = [[AMapLocationManager alloc] init];
    
    [self.locationManager setDelegate:self];
    
    //设置期望定位精度
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
    
    //设置不允许系统暂停定位
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
    //设置允许在后台定位
    //    [self.locationManager se tAllowsBackgroundLocationUpdates:YES];
    
    //开启带逆地理连续定位
    [self.locationManager setLocatingWithReGeocode:YES];
    
    //设置定位超时时间
    [self.locationManager setLocationTimeout:DefaultLocationTimeout];
    
    //设置逆地理超时时间
    [self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
}

- (void)updateLabelTextWithLocation:(CLLocation *)location regeocode:(AMapLocationReGeocode *)regeocode serial:(BOOL)isSerial
{
    NSString *locType = isSerial ? [NSString stringWithFormat:@"连续定位完成:%d", (int)self.locateCount] : @"单次定位完成";
    
    NSMutableString *infoString = [NSMutableString stringWithFormat:@"%@\n\n回调时间:%@\n经 度:%f\n纬 度\t:%f\n精 度:%f米\n海 拔:%f米\n速 度:%f\n角 度:%f\n", locType, location.timestamp, location.coordinate.longitude, location.coordinate.latitude, location.horizontalAccuracy, location.altitude, location.speed, location.course];
    
    if (regeocode)
    {
        NSString *regeoString = [NSString stringWithFormat:@"国 家:%@\n省:%@\n市:%@\n城市编码:%@\n区:%@\n区 域:%@\n地 址:%@\n兴趣点:%@\n", regeocode.country, regeocode.province, regeocode.city, regeocode.citycode, regeocode.district, regeocode.adcode, regeocode.formattedAddress, regeocode.POIName];
        [infoString appendString:regeoString];
        
        
        NSLog(@"aa定位地理位置：：：%@",regeoString);
        
        !_locationBlock?:_locationBlock(location,regeocode);
    }
    
    //    if (isSerial)
    //    {
    //        [self.locationgInfoLabel setText:infoString];
    //    }
    //    else
    //    {
    //        [self.singleLocInfoLabel setText:infoString];
    //    }
}

#pragma mark - AMapLocationManager Delegate

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    
    self.locateCount += 1;
    
    [self updateLabelTextWithLocation:location regeocode:reGeocode serial:YES];
}

@end
